#!/usr/local/bin/ruby

require 'fileutils'
require 'yaml'

@command = ARGV.shift

@home_dir = File.expand_path("~")
@contacts_path = File.join(@home_dir, '.contacts.yml')
@config_path = File.join(@home_dir, '.mailjitsu.yml')

if File.exist?(@config_path)
  # We go through this hack because otherwise when symlinking '~/mailjitsu',
  #  ruby is trying to load incorrect paths b/c it doesn't realize the script is actually a symblink.
  @config = YAML.load_file(@config_path)
  require File.join(@config[:app_path], 'config', 'environment')
else
 require File.expand_path(File.dirname(__FILE__) + "/../config/environment") 
end

@using_symlink = (File.basename(__FILE__) == 'mailjitsu')

case @command
when 'setup'
  if File.exist?(@contacts_path)
    puts "~/.contacts.yml already exists -- *not* overwriting it"
  else
    puts "\nSetting up ~/.contacts.yml ... \n"
    File.open(@contacts_path, 'w') do |f|
      f.puts(Hash.new.to_yaml)
    end
  end
  @app_path = File.dirname(File.expand_path(__FILE__)).gsub('script', '')  # worst hack evar
  #puts "App path: #{@app_path}"
  if !File.exist?(@config_path)
    puts "Setting up ~/.mailjitsu.yml ..."
    @config = {:app_path => @app_path}
    
    File.open(@config_path, 'w') do |f|
      f.puts(@config.to_yaml)
    end
  end
  
  @mailer_config_path = File.join(@app_path, 'config', 'mailer.yml')
  if !File.exist?(@mailer_config_path)
    @example_path = File.join(@app_path, 'config', 'mailer.yml.example')
    FileUtils.cp(@example_path, @mailer_config_path)
    puts "\nCopied over config/mailer.yml.example to config/mailer.yml \n\n  Setup almost complete -- edit config/mailer.yml to configure your GMail & application settings.\n"
  end
  puts "\nFor usage, type: ruby mailjitsu.rb\n\n"
when 'email', 'send'
  @target = ARGV.shift
  @subject = ARGV.shift
  @message = ARGV.shift
  if !@target.include?('@')
    # perhaps an alias?
    @contacts = YAML.load_file(@contacts_path)
    if !@contacts.has_key?(@target)
      puts "Could not find target alias: #{@target}\n\n ... or perhaps an invalid email address?"; return
    end
    @target = @contacts[@target]
  end
  
  res = Mailer.deliver_email(@target, @subject, @message)    
  puts "\nRecipient: #{@target}\n"
  puts "Subject: #{@subject}"
  puts "------------------------"
  puts "#{@message}\n\n"
  
when 'add'
  if !File.exist?(@contacts_path)
    puts "~/.contacts.yml does not exit -- run 'ruby mailjitsu.rb setup' to create the file"
  else
    @contacts = YAML.load_file(@contacts_path)
    @alias = ARGV.shift
    @email = ARGV.shift
    if @alias.include?(' ')
      puts "Aliases cannot have spaces.  You attempted: '#{@alias}'.  Try something like 'foo' or 'foo_bar'"; return
    end
    puts "Adding alias '#{@alias}' for email address: #{@email}"
    @contacts[@alias] = @email
    File.open(@contacts_path, 'w') do |f|
      f.puts(@contacts.to_yaml)
    end
  end
when 'contacts', 'list'
  @contacts = YAML.load_file(@contacts_path)
  puts "\n Mailjitsu Contacts\n\n"
  if @contacts.empty?
    puts " -- No contacts are present in ~/.contacts.yml"
  else
    @contacts.each_pair do |k,v|
      puts "\t#{k}\t\t#{v}"
    end
  end
  puts "\n\n"
else
  @usage = <<EOF
  
      Mailjitsu Usage
  
  ruby mailjitsu.rb setup                                             -- initialize the app

  ruby mailjitsu.rb email foo@bar.og "The subject"  "the message"     -- send an email
    or ruby mailjitsu.rb email joe "Subject" "check this out..."      -- send an email to a contact in the DB

  ruby mailjitsu.rb add joe joseph@bash.org                           -- add contact with alias "joe" to ~/.contacts.yml
  
  ruby mailjitsu.rb list                                              -- list contacts

EOF
  @symlink = <<SYM

      Usage Tips
  
  Create a symbolic link to mailjitsu.rb to execute the script simply via '~/mailjitsu':

     ln -s #{File.expand_path(__FILE__)} #{@home_dir}/mailjitsu
     chmod 755 ~/mailjitsu

SYM
  puts @usage
  puts @symlink if !@using_symlink
end